﻿package level4.helpers {
	import flash.display.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 背景跟随主角滚动
	 * */
	public class ScrollHelper {
		
		// Constants:
		// Public Properties:
		// Private Properties:
		
		// Initialization:
		public function ScrollHelper() { }
	
	
		// Public Methods:
		// Protected Methods:
		
		/**
		让scrollBg随followDo2d的移动而滚屏.		
		 * scroolBg : 要滚动的背景
		 * followDo2d : 移动的物体
		 * edgeDistanceX : 到 x边界的距离
		 * edgeDistanceY : 到y边界的距离
		*/
		public static function scrollWithDo2d(scrollBg:DisplayObject,followDo2d:DisplayObject,edgeDistanceX:int = 480,edgeDistanceY:int = 288):void{
			var tx:int = followDo2d.x;
			var ty:int = followDo2d.y;
			
			scroll(scrollBg,tx,ty,edgeDistanceX,edgeDistanceY);
		}
		private static function scroll(bg:DisplayObject,tx:int,ty:int,edgeDistanceX:int,edgeDistanceY:int):void{
			if(bg == null) 
				return;
			
			var w:int = bg.width;
			var h:int = bg.height;
			
			{
				/**
				 * 此处解决 bg是do2d的父容器,bg的第一个子类是背景,所带来的问题,
				 * **/
				var con:DisplayObjectContainer = bg as DisplayObjectContainer
				if(con != null){
					var firstChild:DisplayObject = con.getChildAt(0);
					if(firstChild != null){
						w = firstChild.width;
						h = firstChild.height;
					}
				}
			}
			
			//限定下x,y边界
			if(edgeDistanceX > bg.stage.stageWidth/2){
				edgeDistanceX = bg.stage.stageWidth/2;
			}
			if(edgeDistanceY > bg.stage.stageHeight/2){
				edgeDistanceY = bg.stage.stageHeight/2;
			}
			
			//获取 bg在舞台上的坐标,
			var stagePositionX:int = bg.x + tx;
			var stagePositionY:int = bg.y + ty;
			//获取 isoDo的活动范围矩形
			var rightEdge:int = bg.stage.stageWidth - edgeDistanceX;
			var leftEdge:int = edgeDistanceX;
			var topEdge:int = edgeDistanceY;
			var bottomEdge:int = bg.stage.stageHeight - edgeDistanceY;
			
			//水平,右移
			if(stagePositionX >= rightEdge){
				bg.x -= stagePositionX - rightEdge;				//背景向左移动,角色的点与右边界点的差
				if(bg.x < bg.stage.stageWidth - w)		//将背景下限制在舞台的右侧
					bg.x = bg.stage.stageWidth - w;
			}else if(stagePositionX <= leftEdge){
				bg.x += (leftEdge-stagePositionX);		//背景向右移动,左边界与角色的点的差
				if(bg.x >0) 							//将背景的左下角限制在舞台的左侧
					bg.x = 0;
			}
			//垂直方向
			/**
			 下移,角色的y坐标大于下侧的值
			 背景向上移动,角色在舞台的点与下侧的差
			 将背景限制在舞台的下侧
			 */
			if(stagePositionY >= bottomEdge){
				var tmp:Number = stagePositionY - bottomEdge; 
				bg.y -= tmp;
				if(bg.y <= bg.stage.stageHeight - h){
					bg.y =  bg.stage.stageHeight - h;
				}
			}else if(stagePositionY <= topEdge){
				/**
				上移,角色的y值小于上侧的值
				背景下移,上侧与角色在舞台上的点的差
				将背景限制舞台的上侧
				*/
				bg.y += topEdge - stagePositionY;			
				if(bg.y >0){
					bg.y = 0;
				}
			}
		}
		/**
		 * 大背景,小舞台 滚动
		 * 复杂情况: 注册点可以在图元的任何地方.不必非得在左上角
		 * */
		public static function checkLargeBg2(scrollDo2d:DisplayObject):void{
			var rect:Rectangle = scrollDo2d.getBounds(scrollDo2d.parent);
			var tlPt:Point = scrollDo2d.parent.globalToLocal(rect.topLeft);
			var brPt:Point = scrollDo2d.parent.globalToLocal(rect.bottomRight);
			var regPt:Point = scrollDo2d.parent.globalToLocal(new Point(scrollDo2d.x,scrollDo2d.y));
			
			if(rect.left > 0){
				scrollDo2d.x = regPt.x - tlPt.x;
			}else if(rect.right < scrollDo2d.stage.stageWidth){
				scrollDo2d.x = scrollDo2d.stage.stageWidth  - ( brPt.x - regPt.x);
			}
			
			if(rect.top > 0){
				scrollDo2d.y = regPt.y - tlPt.y;
			}else if(rect.bottom < scrollDo2d.stage.stageHeight){
				scrollDo2d.y = scrollDo2d.stage.stageHeight - (brPt.y - regPt.y);
			}
		}
		
		/**
		 * scrollDo2d,背景大过 舞台视口
		 * 简单情况,注册点在左上角
		 * */
		public static function checkLargeBg(scrollDo2d:DisplayObject):void{
			var scrollWidth:int = scrollDo2d.width;
			var scrollHeight:int = scrollDo2d.height;
			//x
			if(scrollDo2d.x > 0 ){
				scrollDo2d.x = 0;
			}else if(scrollDo2d.x < scrollDo2d.stage.stageWidth - scrollWidth){
				scrollDo2d.x = scrollDo2d.stage.stageWidth - scrollWidth;
			}
			//y
			if(scrollDo2d.y > 0 ){
				scrollDo2d.y = 0;
			}else if(scrollDo2d.y < scrollDo2d.stage.stageHeight - scrollHeight){
				scrollDo2d.y = scrollDo2d.stage.stageHeight - scrollHeight;
			}
		}
		
		/**
		 * 检测边界.
		 * 小背景,大舞台的情况
		 * */
		public static function checkBounds(scrollDo2d:DisplayObject):void{
			var scrollHalfWidth:int = scrollDo2d.width * .5;
			var scrollHalfHeight:int = scrollDo2d.height * .5;
			//x
			if(scrollDo2d.x < 0 ){
				scrollDo2d.x = 0;
			}else if(scrollDo2d.x > scrollDo2d.stage.stageWidth - scrollHalfWidth){
				scrollDo2d.x = scrollDo2d.stage.stageWidth - scrollHalfWidth;
			}
			//y
			if(scrollDo2d.y < 0 ){
				scrollDo2d.y = 0;
			}else if(scrollDo2d.y + scrollHalfHeight > scrollDo2d.stage.stageHeight){
				scrollDo2d.y = scrollDo2d.stage.stageHeight - scrollHalfHeight;
			}
		}
		
	}
	
}